@echo off

:parameters
rem Descriptive script name
set NAME=oxmidi

set MSBUILD="C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe"
set ILMERGE="utils\ilmerge.exe"
set CONFIG=Release
set PLATFORM=AnyCPU

rem /v:<verbosity> - q[uiet], m[inimal], n[ormal], d[etailed], diag[nostic]
set VERBOSE=m

set SOLUTION=oxmidi.sln
rem CWD - Current working directory, should be the solution directory when publishing, but can be other when deploying
set CWD=%CD%
rem CPU_CORE_COUNT - Number of CPU cores for faster builds
set CPU_CORE_COUNT=2

:validation
if not "%1"=="" goto %1
if "%CWD%"=="" goto error-cwd-missing
if not exist %MSBUILD% goto error-msbuild-missing
if not exist %SOLUTION% goto error-solution-missing
goto default

:clean-all
if not exist %SOLUTION% goto error-solution-missing
echo Cleaning...
for /F %%i in ('dir /s /b *.csproj') do (
	echo Cleaning %%i
	%MSBUILD% %%i /t:Clean /p:WarningLevel=1;Configuration=Debug /v:%VERBOSE% /m:%CPU_CORE_COUNT% /nologo
	%MSBUILD% %%i /t:Clean /p:WarningLevel=1;Configuration=Release /v:%VERBOSE% /m:%CPU_CORE_COUNT% /nologo
)
goto end

:build-all
if not exist %SOLUTION% goto error-solution-missing
echo Building...
for /F %%i in ('dir /s /b *.csproj') do (
	echo Building %%i
	%MSBUILD% %%i /t:Build /p:WarningLevel=0;Configuration=Debug /v:q /m:%CPU_CORE_COUNT% /nologo
	%MSBUILD% %%i /t:Build /p:WarningLevel=0;Configuration=Release /v:q /m:%CPU_CORE_COUNT% /nologo
)
goto end

:unbind
rem Remove all files for SourceSafe reference
echo Removing all SourceSafe files...
del /S /F /Q *.vssscc
del /S /F /Q *.vspscc
del /S /F /Q *.scc
del /S /F /Q *.suo
del /S /F /Q *.user
del /S /F /Q /A:H *.scc
del /S /F /Q /A:H *.suo
for /F %%i in ('dir /S /B /A:H .svn') do rmdir /S /Q %%i
goto end

:default
:build
:oxmidi
if not exist %SOLUTION% goto error-solution-missing
if "%1"=="oxmidi" set CONFIG=%2
if "%1"=="oxmidi" set PLATFORM=%3
if "%CONFIG%"=="" set CONFIG=Release
if "%PLATFORM%"=="" set PLATFORM=AnyCPU
echo Building '%NAME%' (%CONFIG%^|%PLATFORM%)...

set CSPROJ_PATH=source\oxmidi\oxmidi.csproj
set CSPROJ_REL=..\..\
set OUT_DIR=bin
set OUT_EXE_MERGED=bin\oxmidi_merged.exe

if exist "%OUT_DIR%" rmdir /S /Q "%OUT_DIR%"
%MSBUILD% %CSPROJ_PATH% /t:Rebuild /p:OutDir=%CSPROJ_REL%%OUT_DIR%\ /p:WarningLevel=1;Configuration=%CONFIG%;Platform=%PLATFORM% /v:%VERBOSE% /m:%CPU_CORE_COUNT% /nologo
rem /t:<project>:<target>
rem /p:<property1>;<property2>;WarningLevel=1;Configuration=%CONFIG%;OutputDir=bin\Debug
rem /v:<verbosity>     - q[uiet], m[inimal], n[ormal], d[etailed], diag[nostic]
rem /m:2               - number of msbuild.exe processes, 2 for dual core
rem /fl                - file logger, create msbuild.log
rem /nologo            - hides Microsoft (c) ... message
rem /fileLoggerParameters:LogFile=MyLog.log;Append;Verbosity=diagnostic;Encoding=UTF-8
rem /noconsolelogger   - if running from background script

del /S /F /Q %OUT_DIR%\*.pdb

rem Skip merging
cd %CWD%
goto end

echo Merging to '%OUT_EXE_MERGED%'...
rem move %OUT_DIR%\oxmidi.exe %OUT_DIR%\oxmidi_merged.exe
if exist %ILMERGE% (
	%ILMERGE% /target:exe /out:%OUT_EXE_MERGED% /zeroPeKind %OUT_DIR%\oxmidi.exe %OUT_DIR%\Oxage.Common.dll %OUT_DIR%\Oxage.Midi.dll %OUT_DIR%\Oxage.Midi.SanfordPlugin.dll %OUT_DIR%\Oxage.Midi.ToubPlugin.dll %OUT_DIR%\DirectMidiNet.dll %OUT_DIR%\Sanford.Collections.dll %OUT_DIR%\Sanford.Multimedia.dll %OUT_DIR%\Sanford.Multimedia.Midi.dll %OUT_DIR%\Sanford.Multimedia.Timers.dll %OUT_DIR%\Sanford.Threading.dll
	del /S /F /Q %OUT_DIR%\*.dll
	del /S /F /Q %OUT_DIR%\*.pdb
	del /S /F /Q %OUT_DIR%\*.xml
)
rem del /F /Q %OUT_DIR%\oxmidi_merged.exe

cd %CWD%
goto end

:error-solution-missing
echo Error: Cannot find %SOLUTION%
echo Search location: %CD%
echo Tip: copy this script to the solution directory
goto end

:error-msbuild-missing
echo Error: Cannot find MSBuild.exe
echo Search location: %MSBUILD%
goto end

:error-cwd-missing
echo Error: cannot get current working directory
goto end

:end-error
echo Error!
goto end

:end